/*
 * Copyright (c) xiaohe
 */

package com.xh.core.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;

public class MathUtils {

    public static final Integer DEFAULT_SCALE = 1;

    /**
     * 两个数相除,取商
     *
     * @param a     被除数
     * @param b     除数
     * @param scale 保留小数位数(默认一位)
     * @return
     */
    public static double divTwoNumberRoundHalfUp(Integer a, Integer b, Integer scale) {
        if (a == null || b == null || a == 0 || b == 0) {
            return 0;
        }
        if (scale == null) {
            scale = DEFAULT_SCALE;
        }
        double c = (double) a / b;
        BigDecimal bd = new BigDecimal(c);
        bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }

    /**
     * 两个数相除,取商
     *
     * @param a     被除数
     * @param b     除数
     * @param scale 保留小数位数(默认一位)
     * @return
     */
    public static double divTwoNumberRoundHalfUp(Double a, Integer b, Integer scale) {
        if (a == null || b == null || a == 0 || b == 0) {
            return 0;
        }
        if (scale == null) {
            scale = DEFAULT_SCALE;
        }
        double c = (double) a / b;
        BigDecimal bd = new BigDecimal(c);
        bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }

    /**
     * 两个数相除,取商
     *
     * @param a     被除数
     * @param b     除数
     * @param scale 保留小数位数(默认一位)
     * @return
     */
    public static double divTwoNumberRoundHalfUp(Long a, Long b, Integer scale) {
        if (a == null || b == null || a == 0 || b == 0) {
            return 0;
        }
        if (scale == null) {
            scale = DEFAULT_SCALE;
        }
        double c = (double) a / b;
        BigDecimal bd = new BigDecimal(c);
        bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }

    /**
     * 两个数相除,取商
     *
     * @param a     被除数
     * @param b     除数
     * @param scale 保留小数位数(默认一位)
     * @return
     */
    public static double divTwoNumberRoundHalfUp(Long a, Integer b, Integer scale) {
        if (a == null || b == null || a == 0 || b == 0) {
            return 0;
        }
        if (scale == null) {
            scale = DEFAULT_SCALE;
        }
        double c = (double) a / b;
        BigDecimal bd = new BigDecimal(c);
        bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }

    /**
     * 根据参数获取百分比
     * @param a
     * @return
     */
    public static double getNumberRate(Double a,Integer scale){
        if (a == null || a == 0) {
            return 0;
        }
        if (scale == null) {
            scale = DEFAULT_SCALE;
        }
        BigDecimal bd = new BigDecimal(a * 100);
        bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }

    public static void main(String[] args) {
        Integer a = new Integer(1);
        Integer b = new Integer(3);
        double c = divTwoNumberRoundHalfUp(a, b, 3);
        double d = getNumberRate(c, DEFAULT_SCALE);
        System.out.println(d);
    }


    /**
     * 获得数值,为null返回0
     * @param a
     * @return
     */
    public static Integer getValueIgnoreNull(Integer a){
        if (a == null)
            return 0;
        return a;
    }

    /**
     * 获得数值,为null返回0
     * @param a
     * @return
     */
    public static Double getValueIgnoreNull(Double a){
        if (a == null)
            return 0D;
        return a;
    }

    /**
     * 获得数值,为null返回0
     * @param a
     * @return
     */
    public static Long getValueIgnoreNull(Long a){
        if (a == null)
            return 0L;
        return a;
    }

    /**
     * 获得数值,为null返回0
     * @param a
     * @return
     */
    public static Long getValueIgnoreNull(String a){
        if (StringUtils.isBlank(a))
            return 0L;
        return Long.parseLong(a);
    }

    /**
     * 获取集合Size
     * @param c
     * @return
     */
    public static Integer getCollectionSize(Collection c){
        if (c == null) {
            return 0;
        }
        return c.size();
    }

    /**
     * 获取MapSize
     * @return
     */
    public static Integer getMapSize(Map m){
        if (m == null) {
            return 0;
        }
        return m.size();
    }



}
